/*
 * Timer.cpp
 *
 */

#include "Timer.h"

#include <windows.h>

Timer::Timer()
{
	if (!QueryPerformanceFrequency((LARGE_INTEGER *) &frequency)) {
		performanceTimer	= false;
		resolution			= 1.0f/1000.0f;
		frequency			= 1000;
		lastTime			= GetTickCount();
	}
	else {
		QueryPerformanceCounter((LARGE_INTEGER *) &lastTime);
		performanceTimer = true;
		resolution = (float)(((double)1.0f)/((double)frequency));
	}

	fps = 0;
	fpsTime = 0;
	frameCount = 0;

}

Timer::~Timer() {

}

float Timer::getTime() {
	if (performanceTimer) {
		QueryPerformanceCounter ((LARGE_INTEGER *) &time);
	}
	else {
		time = GetTickCount();
	}
	deltaTime = ((float)(time-lastTime)*resolution)*1000.0f;
	lastTime = time;

	fpsTime += deltaTime;
	frameCount++;
	if (fpsTime > 1000) {
		fps = frameCount/fpsTime*1000.0f;
		frameCount = 0;
		fpsTime = 0;
	}

	/* This means windows was inactive, it should not do a big time jump */
	if (deltaTime > 500)
		return 100;
	else
		return deltaTime;
}

void Timer::renderGL()
{
	glColor3f(1.0f,0.0f,0.0f);
	glPrint((int)p_X, (int)p_Y, "FPS - %7.2f", fps);	// Print GL Text To The Screen
}

void Timer::initializeGL()
{
	buildFont();
}


